<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/WEB-INF/jsp/commons/taglibs.jsp" %>
<script>
    $(document).ready(function () {
        //聚焦第一个输入框
        $("#name").focus();
        //为inputForm注册validate函数
        $("#inputForm").validate({
            rules: {
                name: {
                    required: true,
                    remote: {
                        type: "POST",
                        url: "${ctx}/adoms/marchandise/repeat",
                        data: {
                            value: function () {
                                return $.trim($("#name").val());
                            },
                            id: ${marchandise.id == null ? 0:marchandise.id},
                            property: "name"
                        }
                    }
                },
                code: {
                    required: true,
                    remote: {
                        type: "POST",
                        url: "${ctx}/adoms/marchandise/repeat",
                        data: {
                            value: function () {
                                return $.trim($("#code").val());
                            },
                            id: '${marchandise.id == null ? 0:marchandise.id}',
                            property: "code"
                        }
                    }
                },
                description: "required",
                packageing: "required",
                price: {
                    required: true,
                    number: true
                },
                shelflife: "required",
                spcification: "required",
                type: "required",
                dan: "required",
                weight: {
                    required: true,
                    number: true
                },
                producePlace: "required",
                brand: "required",
                barCode: {
                    required: true,
                    minlength: 13,
                    number: true
                },
                numOfBox: {
                    required: true,
                    number: true
                }
            },
            messages: {
                name: {
                    remote: $.format("商品名称重复，请重新输入！")
                }, code: {
                    remote: $.format("商品编号重复，请重新输入！")
                }
            }
        });
        //设置数量/件后面显示的单位
        $("[name=packageing]").click(function () {
            $("#packageing").html("(" + $(this).val() + ")");
            createSpcification();
        });
        //自动生成规格
        $("#weight").keyup(function () {
            createSpcification();
        });
        $("#weight").keyup(function () {
            createSpcification();
        });
        $("#numOfBox").keyup(function () {
            createSpcification();
        });
    });
    function createSpcification() {
        var spcification = $.trim($("#weight").val()) + "克*" + $.trim($("#numOfBox").val()) + $("#packageing").html().replace(")", "").replace("(", "") + "/件";
        $("#spcification").val(spcification);
    }
</script>
<form id="inputForm" class="well form-inline" action="${ctx}/adoms/marchandise/save" method="post">
    <input type="hidden" name="id" value="${marchandise.id}"/>
    <input type="hidden" name="point" value="${marchandise.point}"/>
    <table class="table table-striped table-bordered table-condensed">
        <thead>
        <i class="icon-flag"></i>
        <strong>商品表单</strong>
        </thead>
        <tr>
            <td class="span2">商品名称:</td>
            <td>
                <input id="name" name="name" class="span6" type="text" value="${marchandise.name}" size="30"
                       maxlength="80"/>
            </td>
        </tr>
        <tr>
            <td>商品编码:</td>
            <td>
                <input id="code" class="span6" name="code" type="text" value="${marchandise.code}" size="30"
                       maxlength="80"/>
            </td>
        </tr>
        <tr>
            <td>商品类型:</td>
            <td>
                <select id="type" name="type.id">
                    <c:forEach items="${typeList }" var="type">
                        <option value="${type.id }"
                                <c:if test="${marchandise.type.id == type.id}">selected</c:if>>${type.name }</option>
                    </c:forEach>
                </select>
            </td>
        </tr>
        <tr>
            <td>品牌:</td>
            <td>
                <select id="brand" name="brand.id">
                    <c:forEach items="${brandList }" var="b">
                        <option value="${b.id }"
                                <c:if test="${b.id == marchandise.brand.id }">selected</c:if>>${b.name }</option>
                    </c:forEach>
                </select>
            </td>
        </tr>
        <tr>
            <td>条形码:</td>
            <td>
                <input id="barCode" name="barCode" type="text" value="${marchandise.barCode}" size="30" maxlength="13"/>
            </td>
        </tr>
        <tr>
            <td>商品描述:</td>
            <td>
                <textarea id="description" name="description" rows="2"
                          class="span6">${marchandise.description}</textarea>
            </td>
        </tr>
        <tr>
            <td>包装:</td>
            <td>
                <label class="radio">
                    <input name="packageing" type="radio"
                    <c:if test="${marchandise.packageing == null || marchandise.packageing == '盒' }">
                           checked="checked"
                    </c:if> value="盒">
                    盒
                </label>
                <label class="radio">
                    <input name="packageing" type="radio"
                    <c:if test="${ marchandise.packageing == '听' }">
                           checked="checked"
                    </c:if> value="听">
                    听
                </label>
                <label class="radio">
                    <input name="packageing" type="radio"
                    <c:if test="${ marchandise.packageing == '袋' }">
                           checked="checked"
                    </c:if> value="袋">
                    袋
                </label>
            </td>
        </tr>
        <tr>
            <td>每件数量:</td>
            <td>
                <input id="numOfBox" name="numOfBox" class="span1" type="text" value="${marchandise.numOfBox}" size="30"
                       maxlength="80"/>
                <span class="tBlue"
                      id="packageing">(${ marchandise.packageing == null ? '盒': marchandise.packageing})</span>
            </td>
        </tr>
        <tr>
            <td>单价:</td>
            <td>
                <input id="price" name="price" type="text" value="${marchandise.price}" size="30" maxlength="80"/>
                (元)
            </td>
        </tr>
        <tr>
            <td>净重:</td>
            <td>
                <input id="weight" name="weight" type="text" value="${marchandise.weight}" size="30" maxlength="80"/>
                克/(g)
            </td>
        </tr>
        <tr>
            <td>规格:</td>
            <td>
                <input id="spcification" readonly="readonly" name="spcification" type="text"
                       value="${marchandise.spcification}" size="30" maxlength="80"/>
            </td>
        </tr>
        <tr>
            <td>保质期:</td>
            <td>
                <input id="shelflife" name="shelflife" type="text" value="${marchandise.shelflife}" size="30"
                       maxlength="80"/>
                (月)
            </td>
        </tr>
        <tr>
            <td>段位:</td>
            <td>
                <input id="dan" name="dan" type="text" value="${marchandise.dan}" size="30" maxlength="80"/>
            </td>
        </tr>
        <tr>
            <td>产地:</td>
            <td>
                <input id="producePlace" name="producePlace" type="text" value="${marchandise.producePlace}" size="30"
                       maxlength="80"/>
            </td>
        </tr>
        <tr>
            <td>备注:</td>
            <td>
                <textarea id="memo" name="memo" rows="4" class="span6">${marchandise.memo}</textarea>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input class="btn" type="submit" value="提交"/>
                &nbsp;
                <input class="btn" type="button" value="返回" back="back"/>
            </td>
        </tr>
    </table>
</form>
